package com.cyt.cytcrm.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyt.cytcrm.entity.Employee;
import com.cyt.cytcrm.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author PIGS
 * @since 2020-03-23
 */
@RestController
public class EmployeeController {

    @Autowired
    private IEmployeeService service;
    /**
     *  查询一页员工信息
     *   url :  /emp/每页条数/当前页码
     *          /emp/10/1
     */
    @GetMapping("/emp")
    public Map getList( Integer page, Integer limit ){
        Map<String,Object> map = new HashMap<String,Object>();
        Page<Employee> p = new Page<Employee>();
        p.setSize(limit);
        p.setCurrent(page);

        IPage<Employee> iPage=service.selectList(p);
        map.put("count",iPage.getTotal());
        map.put("data",iPage.getRecords());
        map.put("code",0);
        return map;
    }

    /**
     *  新增用户
     */
    @PostMapping("/emp")
    public Map add(Employee employee,Integer roleId){
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("state",service.add(employee,roleId));
        return result;
    }

    /**
     *  删除用户
     */
    @DeleteMapping("/emp/del/{empId}")
    public Map del(@PathVariable Integer empId){
        Employee employee = new Employee();
        employee.setEmpId(empId);
        employee.setIsDel(1);
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("state",service.updateById(employee));
        return result;
    }

    /**
     *  修改用户
     */
    @PutMapping("/emp")
    public Map edit(Employee employee,Integer roleId){
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("state",service.update(employee,roleId));
        System.out.println(roleId);
        System.out.println(employee);
        return result;
    }

}
